<?php declare(strict_types=1);


namespace App\Services\Easypay\Incoming;

use App\Models\EasypayConfig;
use App\Models\Store;
use App\Models\StoreBank;
use App\Models\StoreImg;
use App\Util\UploadImgUtil;
use Illuminate\Support\Facades\Log;

class PicListService
{
//
//if ($Store->store_type == '1') { //1-个体工商户
//$mer_mode = '1';
//} elseif ($Store->store_type == '2') { //2-企业
//$mer_mode = '0';
//} else { //3-小微商户
//    $mer_mode = '2';
//}
//'store' => $Store,
//'img'   => $StoreImg,
//'bank'  => $StoreBank,
//'cate'  => $MyBankCategory,
    //易生进件图片上传处理
    public static function collect(EasypayConfig $config, string $mer_mode, Store $store, StoreImg $storeImg, StoreBank $storeBank, $myBankCategory): array
    {
        try {
            // 企业	个体	小微  Y  Y  Y
            $list[] = [
                'picMode' => '01', //法人身份证正面,   Y  Y  Y
                'fileId' => self::uploadImage($config, $storeImg->head_sfz_img_a, '01', true, "法人身份证正面"), //图片唯一ID
            ];


            $list[] = [
                'picMode' => '02', //02-法人身份证反面；   Y  Y  Y
                'fileId' => self::uploadImage($config, $storeImg->head_sfz_img_b, '02', true, "法人身份证反面"),
            ];


            $list[] = [
                'picMode' => '03', //03-商户联系人身份证正面；  Y  Y  Y
                'fileId' => self::uploadImage($config, $storeImg->head_sfz_img_a, '03', true, "商户联系人身份证正面"),//
            ];


            $list[] = [
                'picMode' => '04', //04-商户联系人身份证反面；  Y  Y  Y
                'fileId' => self::uploadImage($config, $storeImg->head_sfz_img_b, '04', true, "商户联系人身份证反面"),//
            ];


            $bool = $mer_mode == "2" ? false : true;
            $list[] = [
                'picMode' => '05', //04-营业执照	；  Y  Y N
                'fileId' => self::uploadImage($config, $storeImg->head_sfz_img_b, '04', $bool, "商户联系人身份证反面"),//
            ];


            $list[] = [
                'picMode' => '06', //06-收银台；Y Y Y
                'fileId' => self::uploadImage($config, $storeImg->head_sfz_img_b, "06", true, "收银台照片"),
            ];


            $list[] = [
                'picMode' => '07', //07-内部环境照； Y Y Y
                'fileId' => self::uploadImage($config, $storeImg->store_img_b, '07', true, "内部环境照"),
            ];

            $list[] = [
                'picMode' => '08', //08-营业场所门头照；
                'fileId' => self::uploadImage($config, $storeImg->store_logo_img, '08', true, "营业场所门头照"),
            ];

//            $bool = $mer_mode == "0" ? true : false;
//            $list[] = [
//                'picMode' => '09', //09-门牌号；
//                'fileId' => self::uploadImage($config, $storeImg->house_number, '09', $bool, "门牌号"),//
//            ];

            $list[] = [
                'picMode' => '10', //10-协议；Y Y Y
                'fileId' => self::uploadImage($config, $storeImg->store_other_img_c, '10', true, "协议"),//
            ];

            $bool = $storeBank->store_bank_type == '01' ? false : true;
            $list[] = [
                'picMode' => '13', //14-银行卡
                'fileId' => self::uploadImage($config, $storeImg->store_industrylicense_img, '14', $bool, "开户许可证")//
            ];


            $list[] = [
                'picMode' => '14', //14-银行卡
                'fileId' => self::uploadImage($config, $storeImg->bank_img_a, '14', true, "对私账户银行卡")//
            ];

            $bool = $storeBank->store_bank_type == '01' ? (empty($storeImg->store_auth_bank_img) ? false : true) : false;
            $list[] = [
                'picMode' => '15', //14-银行卡
                'fileId' => self::uploadImage($config, $storeImg->store_auth_bank_img, '14', $bool, "清算授权书")//
            ];
            foreach ($list as $k => $v) {
                if (empty($v["fileId"])) {
                    unset($list[$k]);
                }
            }
            return $list;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }


    public static function uploadImage(EasypayConfig $config, string $img, string $picMode, bool $required, string $message): string
    {
        try {
            if (empty($img)) {
                if ($required == true) {
                    throw new \Exception($message . "不能为空");
                }
                return "";
            }
            $data = [
                'version' => '1.0',
                'fileName' => UploadImgUtil::images_get($img),
                'clientCode' => $config->client_code,
                'picMode' => $picMode,
            ];

            $params = $data;
            unset($data['fileName']);
            $params['MAC'] = Config::sign($config, $data);
//            $params['fileName'] = new \CURLFile($params['fileName']);
            $params['fileName'] = new \CURLFile($params['fileName'], mime_content_type($params['fileName']), basename($params['fileName']));

            $headers = ["Content-Type: multipart/form-data;"];

            $rs = json_decode(Config::curl(Config::getDomain() . Config::IMG_UPDATE, $params, $headers), true);

            Log::channel("easypay")->info("easypay-updateImg", [
                "url" => Config::getDomain() . Config::IMG_UPDATE,
                "param" => $data,
                "req" => $params,
                "res" => $rs
            ]);
            if (isset($rs['fileId']) && !empty($rs['fileId'])) {
                return $rs['fileId'];
            }
            return '';
        } catch (\Exception $ex) {
            throw new \Exception($ex->getMessage());
        }
    }


}